package com.starblink.search.category.ui.newlist

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.base.BaseViewModel
import com.starblink.android.basic.config.StorageKeys
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.basic.storage.SkStorage
import com.starblink.rocketreserver.FetchSaleZoneForegroundCategoryQuery
import com.starblink.rocketreserver.FetchSaleZoneForegroundCategoryV2Query
import com.starblink.rocketreserver.FetchSubForegroundCategoryQuery
import com.starblink.rocketreserver.GetSubFrontCategoriesByCategoryIdQuery
import com.starblink.rocketreserver.fragment.CategoryD
import com.starblink.rocketreserver.fragment.CategoryF
import com.starblink.rocketreserver.type.SaleZoneCategoryReq
import com.starblink.search.category.data.CategoryModel
import com.starblink.search.category.data.CategoryWithParentId
import kotlinx.coroutines.launch

class NewListVM : BaseViewModel() {


    val contentList = arrayListOf<CategoryModel>()
    val liveData = MutableLiveData<List<CategoryModel>>()

    var initCategoryId: String? = ""

    var initExpandIndex: Int? = null

    var initExpandModel: CategoryModel? = null

    val contentLD = MutableLiveData<MutableList<CategoryWithParentId>>(mutableListOf())

    fun loadInitData() {

        viewModelScope.launch {
            val oneCateList = SkGqlClient.skQuery(
                FetchSubForegroundCategoryQuery("0"), showDefaultLoading = true
            )?.data?.fetchSubForegroundCategory?.map { CategoryModel(it.categoryF) }
            if (oneCateList?.isNotEmpty() == true) {
                if (SkStorage.getBoolean(StorageKeys.SEARCH_SALE_ZONE, false)) {
                    val categoryF = CategoryF("0", "Sales Zone", 1, "", "", true, "", "", "", "")
                    val categoryModel = CategoryModel(categoryF, false, null)
                    contentList.add(categoryModel)
                }

                contentList.addAll(oneCateList)
            }
            if (contentList.isNotEmpty()) {
                if (initCategoryId?.isNotEmpty() == true) {
                    contentList.indexOfFirst { categoryModel -> categoryModel.source.id?.toString() == initCategoryId }
                        .takeIf { it > -1 }?.let {
                            val model = contentList[it]
                            model.expand = true
//                            model.children = children
//                            contentList.addAll(it + 1, children)
                            initExpandModel = model
                            initExpandIndex = it
                        }
                } else {
                    var model = contentList.first()
                    model.expand = true
                }
            }
            liveData.postValue(contentList)
            loadSuccess()
        }

    }
}